from django.template import Library
from django.shortcuts import reverse

from web import models

register = Library()


# all_project_list = register.inclusion_tag('inclusion/all_project_list.html')(all_project_list)
@register.inclusion_tag('inclusion/all_project_list.html')
def all_project_list(request):
    # 1. 获取我创建的所有项目
    my_project_list = models.Project.objects.filter(creator=request.tracer.user)
    # 2. 获取我参与的所有项目
    join_project_list = models.Project.objects.filter(projectuser__user=request.tracer.user)
    # 视图view没有render到all_project_list.html，视图view没有render到，所以需要自己传参进去{...., 'request': request}
    return {'my_project_list': my_project_list, 'join_project_list': join_project_list, 'request': request}


@register.inclusion_tag('inclusion/manage_menu_list.html')
def manage_menu_list(request):
    data_list = [
        {'title': '概览', 'url': reverse('web:dashboard', args=(request.tracer.project.id, ))},
        {'title': '问题', 'url': reverse('web:issues', args=(request.tracer.project.id, ))},
        {'title': '统计', 'url': reverse('web:statistics', args=(request.tracer.project.id, ))},
        {'title': '文件', 'url': reverse('web:file', args=(request.tracer.project.id,))},
        {'title': 'wiki', 'url': reverse('web:wiki', args=(request.tracer.project.id, ))},
        {'title': '配置', 'url': reverse('web:setting', args=(request.tracer.project.id, ))},
    ]

    for item in data_list:
        # 当前用户访问的URL: request.path_info  /manage/4/issues/xxx/add/
        if request.path_info.startswith(item['url']):
            item['class'] = 'active'
    return {'data_list': data_list}
